// Schema主要用于 "定义" MongoDB中集合Collection里文档document的结构
// 每个schema会 "映射" 到mongodb中的一个collection，schema "不具备" 操作数据库的能力
// 再由Model模型对数据库进行操作

/*define的数据类型
String      字符串
Number      数字
Date        日期
Buffer      二进制
Boolean     布尔值
Mixed       混合类型
ObjectId    对象ID
Array       数组*/


const mongoose = require('./connect');//导入Model 构造器
const Schema = mongoose.Schema;//构造函数

// 实例化的时候数据尽量写全(内部也要写 ---  "错误:内部不要写");如没写的,不能对其操作。 
// 不过后期需要字段可以再加: MySchema.add({ name: 'string', color: 'string', price: 'number' });
// 声明字段类型有两种方法，一种是首字母大写的字段类型，另一种是引号包含的小写字段类型   String==='string'


function zhushiModel() {
  // Model 模型: 
  // 第一个参数是模型/文档名称(一个参数和其返回值设置为相同的值，否则会出现不可预知的结果!!!!!!);第二个参数为Schema的实例化对象  
  // Model是由Schema编译而成的假想（fancy）构造器，具有抽象属性和行为
  // Model的每一个实例（instance）就是一个document，document可以保存到数据库和对数据库进行操作
  // model是由schema生成的模型，可以对数据库的操作

  /*// 实例方法: Model的实例是document，内置实例方法有很多，如 save，可以通过Schema对象的methods属性给实例自定义扩展方法
  schema.methods.findSimilarSizes = function(cb){
      return this.model('MyModel').find({size:this.size},cb);
  }*/

  /*// 静态方法: 通过Schema对象的statics属性给 Model 添加静态方法
  schema.statics.findByName = function(name,cb){
      return this.find({name: new RegExp(name,'i')},cb);
  }*/

  /*// 两种方法的使用区别: 导入之后使用的
  // statics
  Article.staticFunc ()
  
  //methods
  const article = new Article(arguments)
  article.methodFunc()
  */
}

// Mongoose会将集合名称设置为模型名称的小写版: (在这儿的)大小写组合(在数据库中)都会变成小写的
// user => users //在这儿没有复数;在数据库中对应着他的复数。若结尾是数字就没这个烦恼



//  bug: can not read property of 'images' (item.images.large)多层的时候也许前面的images段不在,发现是schema没写images
let uid0_schema = new Schema({
  username: { type: String },
  id: { type: Number },
});
// 生成集合 -- 数据库会自动生成(且自动加复数,后尾为数字则不加数字)
exports.Uid0 = mongoose.model('uid0', uid0_schema);

let vuer_schema = new Schema({
  uid: Number,// 编号
  userid: String,// 全球唯一码
  username: String,
  phone: String,
  password: String,// 已加密
  member: Date,// 会员有效时间
  createTime: String,
  token: String,
  avatar: String,
  poster: String,
  history: Array,
  collections: Array,
  coupons: Array
});
exports.Vuer = mongoose.model('vuer', vuer_schema);

let comment_schema = new Schema({
  username: String,//session
  m_uid: Number,//uid0
  title: String,//ajax
  content: String,
  filmId: Number,
  name: String,

  time: String,
});
exports.Comment = mongoose.model('comment', comment_schema);

let good_schema = new Schema({
  goods_id: Number,
  goods_name: String,
  short_name: String,
  thumb_url: String,
  hd_thumb_url: String,
  image_url: String,
  sales: Number,
  cnt: Number,
  group_price: Number,
  normal_price: Number,
  market_price: Number,
  event_type: Number,
  customer_num: Number,
  mall_id: Number,
  group: Object,
  sales_tip: String,
  link_url: String,
  hd_url: String,
  quantity: Number,
  activity_type: Number,
  activity_quantity: Number,
  activity_sold_quantity: Number,
  activity_start_time: Number,
  activity_end_time: Number,
  cat_id1: Number,
  goods_type: Number,
  scores: Object,
  group_price_origin: Number,
  deposit_price: Number
});
exports.Good = mongoose.model('good', good_schema);

// sku模板
let goodlist_schema = new Schema({
  _id: String,
  goods_id: String,
  goods_type: String,
  quota: Number,
  quota_used: Number,
  start_sale_num: Number,
  goods_info: Object,
  sku: Object,
  properties: Array,
  initialSku: Object
});
exports.Goodlist = mongoose.model('goodlist', goodlist_schema);

let coupon_schema = new Schema({
  _id: String,
  id: String,
  // 左
  condition: String,
  value: Number,// 1.5元=》150
  valueDesc: String,// 1.5
  unitDesc: String,// 元
  // 右
  name: String,
  startAt: Number,// 时间戳除以100,单位秒
  endAt: Number,
  // 下方
  description: String,
  reason: String,
});
exports.Coupon = mongoose.model('coupon', coupon_schema);

let cartinfo_schema = new Schema({
  _id: String,
  goodsId: String,
  selectedId: Number,
  selectedNum: Number,
  selectedSkuComb: Object,
  messages: Object,
  cartMessages: Object,
  userid: String,
  s1: String,
  s2: String,
  goods_info: Object,
  cartid: String,
  type: {
    type: Number,
    default: 0
  }
});
exports.Cartinfo = mongoose.model('cartinfo', cartinfo_schema);

let addressinfo_schema = new Schema({
  name: String,
  tel: String,
  country: {
    type: String,
    default: 'china'
  },
  province: String,
  city: String,
  county: String,
  areaCode: String,
  postalCode: String,
  addressDetail: String,
  isDefault: Boolean,
  id: String,
  address: String,
  userid: String
});
exports.Addressinfo = mongoose.model('addressinfo', addressinfo_schema);


let cityinfo_schema = new Schema({
  cityId: Number,
  name: String,
  pinyin: String,
  isHot: Number,
  id: String,
  cityEn: String,
  cityZh: String,
  provinceEn: String,
  provinceZh: String,
  leaderEn: String,
  leaderZh: String,
  lat: String,
  lon: String
});
exports.Cityinfo = mongoose.model('cityinfo', cityinfo_schema);

let order_schema = new Schema({
  goods: Array,
  address: Object,
  orderID: String,
  coupon: Object,
  type: {
    type: Number,
    default: 0
  },
  evaluate: String,
  logistics: {
    type: Array,
    default: [
      {
        text: '等待商家处理中',
        time: Date.now()
      }
    ]
  },
  // 直接详情页,还是购物车下单
  orderForm: String,
  userid: String,
  orderTime: {
    // 不能在设置默认的情况下不设置type
    type: Number,//存时间戳,便于排序
    // default: new Date().getTime()
  }
});
exports.Order = mongoose.model('order', order_schema);

// 如果遇到加入数据的数据不一样,可以来看看schema配了没 
